package com.itheima.tanhua.dubbo.api;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.tanhua.api.AnswerApi;
import com.itheima.tanhua.domain.Answer;
import com.itheima.tanhua.dubbo.mappers.AnswerMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lff
 * @date 2022/4/8
 */
@DubboService
public class AnswerApiImpl implements AnswerApi {

    @Autowired
    private AnswerMapper answerMapper;

    /**
     * 获取所有试题答案 并以map集合返回
     *
     * @param problemIds
     * @return
     */
    @Override
    public Map<Long, List<Answer>> findAnswerToMap(List<String> problemIds) {
        Map<Long, List<Answer>> resultMap = new HashMap<>();
        problemIds.forEach(idStr -> {
            //处理id
            Long[] ids = JSON.parseObject(idStr, Long[].class);
            for (Long id : ids) { //遍历problemId查询Answer
                LambdaQueryWrapper<Answer> qw = Wrappers.lambdaQuery(Answer.class);
                qw.eq(Answer::getProblemId, id);
                List<Answer> answers = answerMapper.selectList(qw);
                resultMap.put(id, answers);
            }
        });
        return resultMap;
    }

    //根据答案编号查询对应的分值
    public List<Answer> findAnswerByIds(List<Long> answerIds) {
        LambdaQueryWrapper<Answer> qw = Wrappers.lambdaQuery(Answer.class);
        qw.in(Answer::getId, answerIds);
        return answerMapper.selectList(qw);
    }
}
